package com.biao.java8.demo;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

public class TestLocalDateTime {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println(dateTime);

        // 给定时间
        LocalDateTime dateTime2 = LocalDateTime.of(2021, 9, 19, 21, 15, 01);
        System.out.println(dateTime2);

        LocalDateTime dateTime3 = dateTime.plusYears(1);        // 加1年
        System.out.println(dateTime3);

        LocalDateTime dateTime4 = dateTime.minusMonths(2);       // 减2月
        System.out.println(dateTime4);

        // 分别打印 年,月,日,时,分,秒
        System.out.println(dateTime.getYear());
        System.out.println(dateTime.getMonthValue());
        System.out.println(dateTime.getDayOfMonth());
        System.out.println(dateTime.getHour());
        System.out.println(dateTime.getMinute());
        System.out.println(dateTime.getSecond());


        // 获取两个时间的间隔
        Instant now1 = Instant.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant now2 = Instant.now();
        Duration duration = Duration.between(now1, now2);
        System.out.println(duration.toMillis());

        System.out.println("--------------------");

        // 获取两个日期的间隔
        LocalDate now3 = LocalDate.of(2020, 04, 9);
        LocalDate now4 = LocalDate.now();
        Period period = Period.between(now3, now4);
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());


        System.out.println("------------------------------");
        // 时间校正器
        LocalDateTime now5 = LocalDateTime.now();
        System.out.println(now5);
        LocalDateTime dateTime5 = now5.withDayOfMonth(10);
        System.out.println(dateTime5);
        LocalDateTime with = now5.with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
        System.out.println(with);
        // 自定义下一个工作日
        LocalDateTime localDateTime5 = now5.with(d -> {
            LocalDateTime localDateTime = (LocalDateTime) d;
            DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
            if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {       // 如果是周五
                return localDateTime.plusDays(3);
            } else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {      // 如果是周六
                return localDateTime.plusDays(2);
            } else {
                return localDateTime.plusDays(1);
            }

        });
        System.out.println(localDateTime5);


        // 时间格式化
        DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(dtf);
        System.out.println(format);
        System.out.println("-----------------------------");
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String s = dtf2.format(now);
        System.out.println(s);

        LocalDateTime newDate = now.parse(s, dtf2);
        System.out.println(newDate);
        // 时区处理
        LocalDateTime now6 = LocalDateTime.now(ZoneId.of("Europe/Tallinn"));
        System.out.println(now6);

        ZonedDateTime zone = now6.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zone);
    }
}
